package com.example.administrator.zdtumblr_android_new;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.RelativeLayout;


import com.example.administrator.zdtumblr_android_new.fragment.FourFragment;
import com.example.administrator.zdtumblr_android_new.fragment.MainFragment;
import com.example.administrator.zdtumblr_android_new.fragment.ThreeFragment;
import com.example.administrator.zdtumblr_android_new.fragment.TwoFragment;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends FragmentActivity {

    @Bind(R.id.tab_main1)
    RelativeLayout tabMain1;
    @Bind(R.id.tab_main2)
    RelativeLayout tabMain2;
    @Bind(R.id.tab_main3)
    RelativeLayout tabMain3;
    @Bind(R.id.tab_main4)
    RelativeLayout tabMain4;

    private FragmentManager fm;
    private FragmentTransaction ft;

    private MainFragment mf;
    private TwoFragment tf;
    private ThreeFragment tfr;
    private FourFragment ff;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        setTab(0);
    }

    private void setTab(int i) {
        fm = getSupportFragmentManager();
        ft = fm.beginTransaction();
        hideFragment();
        switch (i) {
            case 0:
                if (mf == null) {
                    mf = new MainFragment();
                    ft.add(R.id.container_main, mf);
                } else {
                    ft.show(mf);
                }
                break;
            case 1:
                if (tf == null) {
                    tf = new TwoFragment();
                    ft.add(R.id.container_main, tf);
                } else {
                    ft.show(tf);
                }
                break;
            case 2:
                if (tfr == null) {
                    tfr = new ThreeFragment();
                    ft.add(R.id.container_main, tfr);
                } else {
                    ft.show(tfr);
                }
                break;
            case 3:
                if (ff == null) {
                    ff = new FourFragment();
                    ft.add(R.id.container_main, ff);
                } else {
                    ft.show(ff);
                }
                break;
        }
        ft.commit();
    }

    private void hideFragment() {
        if (mf != null) {
            ft.hide(mf);
        }
        if (tf != null) {
            ft.hide(tf);
        }
        if (tfr != null) {
            ft.hide(tfr);
        }
        if (ff != null) {
            ft.hide(ff);
        }
    }

    @OnClick({R.id.tab_main1, R.id.tab_main2, R.id.tab_main3, R.id.tab_main4})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tab_main1:
                setTab(0);
                break;
            case R.id.tab_main2:
                setTab(1);
                break;
            case R.id.tab_main3:
                setTab(2);
                break;
            case R.id.tab_main4:
                setTab(3);
                break;
        }
    }
}
